//
//  AppDelegate.m
//  UI17_SandBox
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "AppDelegate.h"
#import "Book.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 沙盒(SandBox)
    // 1. Document (文件文档 用户数据存储)
    // 2. Library (资源 一般用来存放 程序员要存储的一些信息)
    //  1> CaChes (缓存文件的)
    //  2> Preferences (用户信息       设置 NSUserDefaults)
    // 3. tmp (临时目录 下载的临时文件)
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"login"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // 获取 沙盒路径
    // 快速获取到赌赢目录的 c函数
    NSHomeDirectory(); // 根目录
//    NSLog(@"%@", NSHomeDirectory());
    NSTemporaryDirectory(); // 临时目录 tmp
//    NSLog(@"mmmmmmm :%@", NSTemporaryDirectory());
    // C函数
    // 参数1 搜索文件夹路径
    // 参数2 在用户作用域下搜索
    // yes代表绝对路径 no代表相互路径 (基本上都用绝对路径)
    // 1. 创建沙盒路径
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@", pathArray);
    
    // NSBundle 对应的.app文件包
    NSLog(@"ssss%@", [NSBundle mainBundle]);
    
    // 简单文件读写 I 输入 O 输出
    // 字符串的 写入
    NSString *str = @"离别是今晚的康桥, I/O file";
    // 一般拼接路径时 使用 stringByAppendingPathComponent 这种写法不用加"/"
//    NSString *path = [[pathArray firstObject] stringByAppendingString:@"/poet.txt"];
    NSString *path = [[pathArray firstObject] stringByAppendingPathComponent:@"poet.txt"];
    NSError *error = nil;
    BOOL isSuc = [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (isSuc) {
        NSLog(@"存储失败");
    } else {
        NSLog(@"存储成功");
    }
    // 字符串的读取
    NSError *readError = nil;
    NSString *readStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&readError];
    NSLog(@"%@", readStr);
    
    // 将数组写入本地
    NSArray *array = @[@"十年", @"陀飞轮", @"十面埋伏", @"遥远的她"];
    NSString *arrayPath = [[pathArray firstObject]stringByAppendingPathComponent:@"musicName.plist"];
    BOOL isSuce = [array writeToFile:arrayPath atomically:YES];
    if (isSuce) {
        NSLog(@"存储失败");
    } else {
        NSLog(@"存储成功");
    }
    // 读取数组
    NSArray *array1 = [NSArray arrayWithContentsOfFile:arrayPath];
    NSLog(@"%@", array1);
    
    // 字典写入本地
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"十年", @"陈奕迅", @"大地", @"beyond", @"可乐", @"赵", nil];
    NSString *dicPath = [[pathArray firstObject] stringByAppendingPathComponent:@"music.plist"];
    BOOL isDic = [dic writeToFile:dicPath atomically:YES];
    if (isDic) {
        NSLog(@"存储成功");
    } else {
        NSLog(@"存储失败");
    }
    // 读出字典
    NSDictionary *secDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
    NSLog(@"%@", secDic);
    
    // 将NSData类型写入本地
    UIImage *image = [UIImage imageNamed:@"u=881185622,577705802&fm=206&gp=0"];
    // 需要将图转化为二进制(NSData类型)
    // UIImageJPEGRepresentation  UIImagePNGRepresentation(<#UIImage * _Nonnull image#>)
    NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
    NSLog(@"%@", imageData);
    NSString *dataPath = [[pathArray firstObject] stringByAppendingPathComponent:@"image.a"];
    BOOL isData = [imageData writeToFile:dataPath atomically:YES];
    if (isData) {
        NSLog(@"写入成功");
    } else {
        NSLog(@"写入失败");
    }
    // 读取NSData
    NSData *newData = [NSData dataWithContentsOfFile:dataPath];
    UIImage *newImage = [UIImage imageWithData:newData];
    NSLog(@"%@", newImage);
    
    // 复杂对象的读写 自定义的类型(model)
    // 归档/反归档 序列化/反序列化 (专门用于复杂对象存储)
    // 1. 归档(创建对象 将对象存储到本地 归档走的是第二个方法)
    Book *book = [Book new];
    book.bookName = @"傲慢与偏见";
    book.bookPrice = @"18.5";
    book.writer = @"josejjs";
    book.bookType = @"story";
    book.bookAddress = @"English";
    
    NSString *bookPath = [[pathArray firstObject] stringByAppendingPathComponent:@"book.plist"];
    BOOL isBook = [NSKeyedArchiver archiveRootObject:book toFile:bookPath];
    if (isBook) {
        NSLog(@"写入成功");
    } else {
        NSLog(@"写入失败");
    }
    
    // 2. 反归档(取出对象 反归档走的是初始化)
    Book *book1 = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
    NSLog(@"%@", book1.bookName);
    
    
    // 如果对象想要实现归档和反归档
    // 1. 对象对应的类要签订协议(NSCoding)
    
    // 2. 实现协议方法 initWithCoder  encodeWithCoder
    // 3. 归档时使用NSKeyedArchiver
    // 4. 反归档时使用 NSKeyedUnarchiver
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
